package com.amado.course42Nio.demo1;

import java.nio.Buffer;
import java.nio.ByteBuffer;

/**
 * server 端
 *
 * @author wanyulong
 * @since 2018/4/7 14:32
 */
public class ByteBufferTest {
    public static void main(String[] args) {
        System.out.println("duplicate-------------");
//        duplicate();
        System.out.println("slice-------------");
//        slice();
        rewind();
    }

    private static void hello(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(10);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("-----------------------");
        byteBuffer.put((byte)'H').put((byte)'F').put((byte)'A');
        System.out.println(byteBuffer);
        //反转前position=3,limit =10,capacity=10
        //反转后position=0,limit =3(limit=position前的值3),capacity =10
        byteBuffer.flip();
        System.out.println("-----------------------");
        System.out.println(byteBuffer);
        //反转前position=3,limit =10,capacity=10
        //反转后position=0,limit =3(limit=position前的值3),capacity =10
        System.out.println("-----------------------");
        //mark = position=0
        byteBuffer.mark();
        System.out.println(byteBuffer.get());
        System.out.println(byteBuffer.get());
        System.out.println("-----------------------");
        //读取两个
        //读前position=0,limit =3,capacity=10
        //读后position=2,limit =3(limit=position前的值3),capacity =10
        System.out.println(byteBuffer);
        System.out.println("---------重置mark--------------");
        //重置mark,position=0
        byteBuffer.reset();
    }

    /**
     * 复制一个buffer数组，共有buffer
     */
    private static void duplicate(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("-----------------------");
        for (int i=1;i<10;i++){
            byteBuffer.put((byte)i);
        }
        byteBuffer.position(3);
        byteBuffer.limit(5);
        //浅复制，共用byte[]
        printArray(byteBuffer.duplicate());
        System.out.println();
    }

    /**
     * 切割postion-limit之前的数据，但还是共有一个byte数组
     */
    private static void slice(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("-----------------------");
        for (int i=1;i<10;i++){
            byteBuffer.put((byte)i);
        }
        byteBuffer.position(3);
        byteBuffer.limit(5);
        //浅复制，共用byte[]
        printArray(byteBuffer.slice());
        System.out.println();
    }

    /**
     * 复制得到一个只读的buffer
     */
    private static void asReadOnlyBuffer(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("-----------------------");
        for (int i=1;i<10;i++){
            byteBuffer.put((byte)i);
        }
        byteBuffer.position(3);
        byteBuffer.limit(5);
        //会创建一个ByteBufferR,只读的buffer
        printArray(byteBuffer.asReadOnlyBuffer());
        System.out.println();
    }

    /**
     * rewind之后可以重新再读取
     */
    private static void rewind(){
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        System.out.println(byteBuffer.position());
        System.out.println(byteBuffer.limit());
        System.out.println(byteBuffer.capacity());
        System.out.println("-----------------------");
        for (int i=1;i<4;i++){
            byteBuffer.put((byte)i);
        }
        byteBuffer.flip();
        System.out.println("get ="+byteBuffer.get());
        printArray(byteBuffer);
        printArray(byteBuffer.rewind());
        System.out.println("get ="+byteBuffer.get());
        printArray(byteBuffer.rewind());
        System.out.println();
    }

    private static void printArray(Buffer buffer){
        System.out.println(buffer);
    }
    private static void printArray(ByteBuffer byteBuffer){
        System.out.println(byteBuffer);
        for (byte aByte : byteBuffer.array()) {
            System.out.print(aByte+" ");
        }
    }
}
